const Parse = require('parse/node');
const ParseHelper = require('../helpers/parse');
const LogObj = Parse.Object.extend("v5_log");
const UserService = require("./user");

async function addLog(user,type, des) {
    const operator_username = user.username;
    var operator_showname;
    var operator_role;
    if(!user.showName){
        const parseUser = await UserService.getUserInfo(operator_username);
        const userInfo = ParseHelper.toJSON(parseUser)||{};
        operator_showname = userInfo.showName;
        operator_role = userInfo.isAdmin?"管理员":(userInfo.isTeacher?"教师":"学生");
    }else{
        operator_showname = user.showName;
        operator_role = user.isAdmin?"管理员":(user.isTeacher?"教师":"学生");
    }
    const log = {operator_role,operator_username,operator_showname,type, des};
    const parseLog = new LogObj(log);
    await parseLog.save(null, {useMasterKey: true});
    return ParseHelper.toJSON(parseLog);
}


module.exports={
    addLog,
};